﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.App;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.DALFactory;
namespace HKEA.CMS.Core.DAL.App
{
    public class app_tblAddressDAL:Iapp_tblAddress
    {
        HKEA_CMSEntities model;
        app_tblAddress address;
        private app_tblAddressDAL()
        {
            model = DataAccess.GetModelInstance();
        }

        public static app_tblAddressDAL GetInstance()
        {
            return new app_tblAddressDAL();
        }

        public int InsertENT(app_tblAddress ent)
        {
            ent.CreationDate = DateTime.Now;
            ent.LastModifyDate = DateTime.Now;
            model.app_tblAddress.AddObject(ent);
            if (model.SaveChanges() > 0)
                return model.app_tblAddress.Max(a=>a.AppAddressUID);
            return 0;
        }

        public app_tblAddress GetAppENTByENT(app_tblAddress ent)
        {
            var result = model.app_tblAddress.Where(a=>a.AppAddressUID > 0);
            if (ent.AppAddressUID > 0)
                result = result.Where(a=>a.AppAddressUID==ent.AppAddressUID);
            if (ent.AddressUID > 0)
                result = result.Where(a=>a.AddressUID==ent.AddressUID);
            return result.FirstOrDefault();
        }

        public int GetCountByENT(app_tblAddress ent)
        {
            throw new NotImplementedException();
        }

        public int UpdateENT(app_tblAddress ent)
        {
            var source = model.app_tblAddress.Where(a=>a.AppAddressUID ==ent.AppAddressUID).FirstOrDefault();
            if (source != null)
            {
                source.Area_Chi = ent.Area_Chi;
                source.Area_Eng = ent.Area_Eng;
                source.City_Chi = ent.City_Chi;
                source.City_Eng = ent.City_Eng;
                source.CountryUID = ent.CountryUID;
                source.Deleted = ent.Deleted;
                source.Department_Chi = ent.Department_Chi;
                source.Department_Eng = ent.Department_Eng;
                source.District_Chi = ent.District_Chi;
                source.District_Eng = ent.District_Eng;
                source.Flat_Bldg_Chi = ent.Flat_Bldg_Chi;
                source.Flat_Bldg_Eng = ent.Flat_Bldg_Eng;
                source.FullAdd_Chi = ent.FullAdd_Chi;
                source.FullAdd_Eng = ent.FullAdd_Eng;
                source.LastModifyDate = DateTime.Now;
                source.PostalCode = ent.PostalCode;
                source.Province_Chi = ent.Province_Chi;
                source.Province_Eng = ent.Province_Eng;
                source.Street_Rd_Chi = ent.Street_Rd_Chi;
                source.Street_Rd_Eng = ent.Street_Rd_Eng;
                return model.SaveChanges();
            }
            return 0;
        }

        public int DeleteENT(app_tblAddress ent)
        {
            var source = model.app_tblAddress.Where(a=>a.AppAddressUID > 0);
            if (ent.AddressUID > 0)
                source = source.Where(a=>a.AddressUID==ent.AddressUID);
            if (ent.AppAddressUID > 0)
                source = source.Where(a=>a.AppAddressUID==ent.AppAddressUID);
            var list = source.ToList();
            for (int i = 0; i < list.Count; i++)
            {
                model.app_tblAddress.DeleteObject(list[i]);
            }
            return model.SaveChanges();
        }


        public int DeleteByKeyList(List<int> list)
        {
            foreach (var temp in list)
            {
                var result = model.app_tblAddress.Where(a => a.AppAddressUID == temp).FirstOrDefault();
                if (result != null)
                    model.app_tblAddress.DeleteObject(result);
            }
            return model.SaveChanges();
        }
    }
}
